package structures.linklist;

public class DoubleLinkListDemo {
    public static void main(String[] args) {
        DoubleLinkList doubleLinkList = new DoubleLinkList();
        doubleLinkList.add(new FigureNode(1, "宋江"));
        doubleLinkList.add(new FigureNode(5, "鲁智深"));
        doubleLinkList.add(new FigureNode(3, "武松"));
        doubleLinkList.add(new FigureNode(4, "李逵"));
        doubleLinkList.add(new FigureNode(5, "孙二娘"));

        doubleLinkList.showAllNodes();
        System.out.println("------------ 显示node  添加结束 -------------");
        doubleLinkList.updateNode(new FigureNode(3, "武大郎"));
        doubleLinkList.showAllNodes();
        System.out.println("------------ 显示node  修改结束 -------------");
        doubleLinkList.deleteNode(3);
        doubleLinkList.showAllNodes();
        System.out.println("------------ 显示node  删除结束 -------------");



    }
}

class DoubleLinkList {
    // 初始化一个头结点
    private final FigureNode headNode = new FigureNode(0, "");

    // 增 按照id增序添加
    public boolean add(FigureNode newNode) {
        FigureNode node = getHeadNode().getNext();

        // 双链表没有数据则直接添加
        if (node == null) {
            getHeadNode().setNext(newNode);
            newNode.setPre(getHeadNode());
            return true;
        }

        // 遍历双链表  添加数据
        while (true) {
            // 判断id 进行插入
            if (node.getId() > newNode.getId()) {
                newNode.setNext(node);
                newNode.setPre(node.getPre());
                node.getPre().setNext(newNode);
                node.setPre(newNode);

                return true;
            }
            // 遍历到尾端没有数据 则直接插入
            if (node.getNext() == null) {
                node.setNext(newNode);
                newNode.setPre(node);
                // 尾部添加node指向null
                newNode.setNext(null);
                return true;
            }

            node = node.getNext();
        }
    }

    // 根据id查找元素进行修改
    public boolean updateNode(FigureNode newNode) {
        FigureNode node = getHeadNode().getNext();

        while (node != null) {
            if (node.getId() == newNode.getId()) {
                node.setName(newNode.getName());
                return true;
            }
            node = node.getNext();
        }

        return false;
    }

    // 根据id 进行删除
    public boolean deleteNode(int id) {
        FigureNode node = getHeadNode().getNext();
        while (node != null) {
            // 进行删除操作
            if (node.getId() == id) {
                // 此处判断一下是否存在下一个节点  否则会有空指针异常
                if (node.getNext() != null) {
                    node.getNext().setPre(node.getPre());
                }
                node.getPre().setNext(node.getNext());
                node.setPre(null);
                node.setNext(null);
                return true;
            }
            node = node.getNext();
        }
        return false;
    }

    public void showAllNodes() {
        FigureNode node = getHeadNode().getNext();
        while (node != null) {
            System.out.println(node);
            node = node.getNext();
        }
    }

    public FigureNode getHeadNode() {
        return headNode;
    }
}

class FigureNode {
    private int id;
    private String name;
    private FigureNode pre;
    private FigureNode next;

    public FigureNode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public FigureNode getPre() {
        return pre;
    }

    public void setPre(FigureNode pre) {
        this.pre = pre;
    }

    public FigureNode getNext() {
        return next;
    }

    public void setNext(FigureNode next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "FigureNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
